//
//  CZOrderController.m
//  TCProject
//
//  Created by admin on 16/1/27.
//  Copyright © 2016年 wise-wing. All rights reserved.
//

#import "CZOrderController.h"
#import "NoHiligitButton.h"
#import "EmptyView.h"
#import "OrderDetailController.h"
#import "OrderManager.h"
#import "EmptyView.h"
#import "CZOrderCell.h"


@interface CZOrderController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) OrderManager  *orderManager;

@property (nonatomic, strong) NSMutableArray *list1;
@property (nonatomic, strong) NSMutableArray *list2;
@property (nonatomic, strong) NSMutableArray *list3;

@property (nonatomic, strong) EmptyView *emptyView1;
@property (nonatomic, strong) EmptyView *emptyView2;
@property (nonatomic, strong) EmptyView *emptyView3;
@property (weak, nonatomic) IBOutlet NoHiligitButton *allOrderButton;

@property (weak, nonatomic) IBOutlet NoHiligitButton *endOrderButton;

@property (weak, nonatomic) IBOutlet NoHiligitButton *doingOrderButton;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *indicatorLeftMargin;

- (IBAction)menuClick:(UIButton *)sender;

@property (nonatomic, strong) UIButton *currentButton;

@end

@implementation CZOrderController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的订单";
    
    [self configuration];
    
    _list1 = [NSMutableArray array];
    _list2 = [NSMutableArray array];
    _list3 = [NSMutableArray array];
    self.orderManager = [[OrderManager alloc] init];
    
    
}

- (void)configuration {
    weakSelf(self);
    for (int i = 1; i < 4; i++) {
        UITableView *tableView = [[UITableView alloc] init];
        tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [tableView registerNib:[UINib nibWithNibName:@"CZOrderCell" bundle:nil] forCellReuseIdentifier:reuseIdentifier];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.backgroundColor = [UIColor clearColor];
        tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf getData:tableView resset:YES];
        }];
        tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakSelf getData:tableView resset:NO];
        }];
        tableView.footer.automaticallyHidden = YES;
        tableView.rowHeight = 236.f;
        tableView.tag = i;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.frame = _scrollView.bounds;
        tableView.x = (i - 1) * self.view.width;
        [_scrollView addSubview:tableView];
        [tableView.header beginRefreshing];
    }
    _scrollView.contentSize = CGSizeMake(kScreenWidth * 3, 0);
}

- (void)getData:(UITableView *)tableView resset:(BOOL)reset
{
    weakSelf(self);
    if (reset) {
        [self.orderManager resetWithType:tableView.tag];
    }
    [self.orderManager getOwnerOrderList:tableView.tag complete:^(BMResponse *response, NSError *error) {
        if ([tableView.header isRefreshing]) {
            [tableView.header endRefreshing];
        }
        if ([tableView.footer isRefreshing]) {
            [tableView.footer endRefreshing];
        }
        if (response.status == kResultOK) {
            switch (tableView.tag) {
                case 1:
                    if (reset) {
                        [weakSelf.list1 removeAllObjects];
                    }
                    [weakSelf.list1 addObjectsFromArray:response.result];
                    break;
                case 2:
                    if (reset) {
                        [weakSelf.list2 removeAllObjects];
                    }
                    [weakSelf.list2 addObjectsFromArray:response.result];
                    break;
                case 3:
                    if (reset) {
                        [weakSelf.list3 removeAllObjects];
                    }
                    [weakSelf.list3 addObjectsFromArray:response.result];
                    break;
            }
            [tableView reloadData];
        } else {
            if (error.domain == kBMErrorDomain && error.code == -1) {
                [tableView.footer noticeNoMoreData];
            } else {
                //                if (error) {
                //                    [self showHint:@"未知错误"];
                //                } else {
                //                    [self showHint:response.msg];
                //                }
            }
        }
    }];
}


- (IBAction)menuClick:(UIButton *)sender {
    if(_currentButton == sender) return;
    [_scrollView setContentOffset:CGPointMake(_scrollView.width * sender.tag, 0) animated:YES];
}

#pragma mark - UIScrollViewDelegate代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _scrollView) {
        _indicatorLeftMargin.constant = scrollView.contentOffset.x / 3;
        CGFloat index = scrollView.contentOffset.x / self.scrollView.width + 0.5;
        if (index > 0 && index < 1) {
            _doingOrderButton.selected = YES;
            _endOrderButton.selected = NO;
            _allOrderButton.selected = NO;
            _currentButton = _doingOrderButton;
        } else if (index >= 1 && index < 2) {
            _doingOrderButton.selected = NO;
            _endOrderButton.selected = YES;
            _allOrderButton.selected = NO;
            _currentButton = _endOrderButton;
        } else if (index >= 2 && index < 3) {
            _doingOrderButton.selected = NO;
            _endOrderButton.selected = NO;
            _allOrderButton.selected = YES;
            _currentButton = _allOrderButton;
        }
    }
}

- (EmptyView *)emptyView1
{
    if (!_emptyView1) {
        _emptyView1 = [[EmptyView alloc] init];
        _emptyView1.message = @"暂无订单，现在去派单>>";
        _emptyView1.image = [UIImage imageNamed:@"empty_order"];
    }
    return _emptyView1;
}

- (EmptyView *)emptyView2
{
    if (!_emptyView2) {
        _emptyView2 = [[EmptyView alloc] init];
        _emptyView2.message = @"暂无订单，现在去派单>>";
        _emptyView2.image = [UIImage imageNamed:@"empty_order"];
    }
    return _emptyView2;
}

- (EmptyView *)emptyView3
{
    if (!_emptyView3) {
        _emptyView3 = [[EmptyView alloc] init];
        _emptyView3.message = @"暂无订单，现在去派单>>";
        _emptyView3.image = [UIImage imageNamed:@"empty_order"];
    }
    return _emptyView3;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = 0;
    UIView *targetView = nil;
    
    switch (tableView.tag) {
        case 1:
            count = _list1.count;
            targetView = self.emptyView1;
            break;
        case 2:
            count = _list2.count;
            targetView = self.emptyView2;
            break;
        case 3:
            count = _list3.count;
            targetView = self.emptyView3;
            break;
    }
    
    if (!count) {
        targetView.frame = tableView.bounds;
        [tableView addSubview:targetView];
    } else if (count && targetView.superview) {
        [targetView removeFromSuperview];
    }
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CZOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    CZOrder *order;
    switch (tableView.tag) {
        case 1:
            order = _list1[indexPath.row];
            break;
        case 2:
            order = _list2[indexPath.row];
            break;
        case 3:
            order = _list3[indexPath.row];
            break;
    }
    cell.order = order;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OrderDetailController *detailVC = [self.storyboard instantiateViewControllerWithIdentifier:@"orderDetailVC"];
    detailVC.isOwn = YES;
    CZOrder *order;
    switch (tableView.tag) {
        case 1:
            order = _list1[indexPath.row];
            break;
        case 2:
            order = _list2[indexPath.row];
            break;
        case 3:
            order = _list3[indexPath.row];
            break;
    }
    detailVC.order = order;
    [self showViewController:detailVC sender:nil];
}


@end
